var Tile = function(id, heroId, canPass, humidity, fertility, fertilityCap, x, y)
{
    this.id = id;
    this.heroId = heroId;
    this.canPass = canPass;
    this.x = x;
    this.y = y;

    //monte quand il pleut / la tile est arrosée
    this.humidity = humidity;
    //monte tout seul doucement quand aucune plantation sur la tile
    //ou à l'aide d'un fertilizer, a un niveau max par tile qui ne peut être dépassé
    //fertilité random par bandes (80-100 sur 8 tiles, ou 10-20 sur 8 tiles etc)
    this.fertility = fertility;
    this.fertilityCap = fertilityCap;

    //vars définies par le client
    this.img = null;
    this.healthLevel = null;
    this.healthImg = null;
    this.building = null;
    this.crop = null;

    this.drawTile = function() {
        if(this.building != null) {
            this.building.drawBuilding();
//            console.log('drawbuilding called');
        }
        else if(this.crop != null) {
            //On redrawn la tile puis le heal level avant la crop (transparence)
            c.drawImage(this.img,
                this.x + camera.x,
                this.y + camera.y,
                defaultTileWidth, defaultTileHeight);
            this.drawHealthLevel();
            this.crop.drawCrop();
//            console.log('drawCrop called');
        }
        else {
            c.drawImage(this.img,
                this.x + camera.x,
                this.y + camera.y,
                defaultTileWidth, defaultTileHeight);
            this.drawHealthLevel();
        }
    }

    //INIT  (GAME LOAD FROM SERV DATA)
    this.initCrop = function(crop) {
        this.crop = crop;
        this.canPass = 0
    }

    this.initBuilding = function(building) {
        this.building = building;
        this.canPass = 0
    }

    //SETTERS
    this.setBuilding = function(building) {
        this.building = building;
        this.canPass = 0;
//        console.log('tile id '+this.id+' init building at '
//            +findRowCol(this).row+','+findRowCol(this.col));
    }

    this.setCrop = function(crop) {
        this.crop = crop;
        this.canPass = 0;
    }

    this.setPlayer = function(curHero) {
        this.playerName = curHero.name;

        cl_tile_update(this);
    }

    //UTILS

    this.getInfos = function() {
        selectedTile = this;

        var infosText = 'Tile stats :';
        infosText += '<br /> Humidity : ' + this.humidity + '%';
        infosText += '<br /> Fertility : ' + this.fertility + '%';
        infosText += '<br />';

        if(this.crop != null) {
            infosText += this.crop.getInfos();
        }
        else if(this.building != null) {
            infosText += this.building.getInfos();
        }
        else if(this.heroId != hero.id) {
            infosText += '<br/><br/><button onclick="captureTile()">Capture this tile</button>'
        }

        return infosText;
    }

    this.setHealthLevel = function(fertility,humidity) {
        var value = (fertility + humidity) / 2;

        switch(true) {
            case ((value <= 20)):    this.healthLevel = HEALTH.BAD; break;
            case ((value <= 40)):    this.healthLevel = HEALTH.LOW; break;
            case ((value <= 60)):    this.healthLevel = HEALTH.AVERAGE; break;
            case ((value <= 80)):    this.healthLevel = HEALTH.MEDIUM; break;
            case ((value <= 100)):   this.healthLevel = HEALTH.GOOD; break;
            default:               break;
        }
    }

    this.drawHealthLevel = function()
    {
        switch(this.healthLevel) {
            case(HEALTH.GOOD):      this.healthImg = h_good; break;
            case(HEALTH.MEDIUM):    this.healthImg = h_medium; break;
            case(HEALTH.AVERAGE):   this.healthImg = h_average; break;
            case(HEALTH.LOW):       this.healthImg = h_low; break;
            case(HEALTH.BAD):       this.healthImg = h_bad; break;

        }
        c.drawImage(this.healthImg,
            this.x + camera.x,
            this.y + camera.y,
            defaultTileWidth, defaultTileHeight);
    }

    this.updateHealthLevel = function() {
        this.setHealthLevel(this.fertility,this.humidity);
    }

    this.updateProperty = function() {
        //On définit l'image de la tile par rapport à l'appartenance
        switch(this.heroId) {
            case hero.id:
                this.img = tile_owned;
                hero.addTile(this.asSimpleObject());
                break;
            case null:
                this.img = tile_neutral;
                break;
            default :
                this.img = tile_enemy;
                var tileHero = findHeroById(this.heroId);
                if(tileHero != null) tileHero.addTile(this.asSimpleObject());
                break;
        }
    }

    //cl_events JSON debugging
    this.asSimpleObject = function() {
        return {
            id:             this.id,

            heroId:         this.heroId,

            canPass:        this.canPass,

            pos:            {
                row:        findRowCol(this).row,
                col:        findRowCol(this).col
            },

            humidity:       this.humidity,

            fertility:      this.fertility,
            fertilityCap:   this.fertilityCap
        }
    }
}

//
//var TileUtils = function() {
//    this.getById = function(tileMap, id) {
//        for (var row = 0; row < tileMap.length; row++) {
//            for (var col = 0; col < tileMap[row].length; col++) {
//                if(tileMap[row][col].id == id) return tileMap[row][col];
//            }
//        }
//    }
//
//
//}